﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Validity.Test.Rules
{
  [TestClass]
  public class DecoratedRuleTest
  {
    private class ObjectStubValidator
      : ValidatorBase<ObjectStub>
    {
      public ObjectStubValidator()
      {
        DefineRulesFor(stub => stub.MyStringProperty1)
          .IsEqualTo("MyString")
          .WhenUsingComparer((lhs, rhs) => lhs.ToLower().CompareTo((rhs ?? String.Empty).ToLower()))
          .If((String value) => value != null)
          .ReplacingMessageWith("My Custom String Comparer Message");
      }
    }

    private readonly ObjectStubValidator _validator = new ObjectStubValidator();

    [TestMethod]
    public void Test()
    {
      VerifyValidValidationResult(_validator.Validate(new ObjectStub { MyStringProperty1 = null }).First());
      VerifyValidValidationResult(_validator.Validate(new ObjectStub { MyStringProperty1 = "myString" }).First());

      VerifyInvalidValidationResult(_validator.Validate(new ObjectStub { MyStringProperty1 = "myOtherString" }).First());
    }

    private void VerifyValidValidationResult(ValidationResult result)
    {
      Assert.IsTrue(result.IsValid);
      Assert.AreEqual(0, result.RuleViolations.Count());
    }

    private void VerifyInvalidValidationResult(ValidationResult result)
    {
      Assert.IsFalse(result.IsValid);

      var ruleViolations = result.RuleViolations.ToList();

      Assert.AreEqual(1, ruleViolations.Count);
      Assert.AreEqual("My Custom String Comparer Message", ruleViolations[0]);
    }
  }
}
